---
layout: "default"
title: "AnyBidirectionalIndex"
description: "Swift documentation for 'AnyBidirectionalIndex': A wrapper over an underlying BidirectionalIndex that hides
the specific underlying type."
keywords: "AnyBidirectionalIndex,struct,swift,documentation,advanced,advanced,distance,predecessor,successor,Distance"
root: "/v3.0"
---

<div class="intro-declaration"><code class="language-swift">struct AnyBidirectionalIndex</code></div>

<div class="discussion comment">
    <p>A wrapper over an underlying <code>BidirectionalIndex</code> that hides
the specific underlying type.</p>

<p><strong>See Also:</strong> <code>AnyBidirectionalCollection</code></p>
</div>

<table class="standard">
<tr>
<th id="inheritance">Inheritance</th>
<td>
<code class="inherits">BidirectionalIndex, Equatable, ForwardIndex, _Incrementable</code>
<span class="viz"><a href="hierarchy/">View Protocol Hierarchy &rarr;</a></span>
</td>
</tr>

<tr>
<th id="aliases">Associated Types</th>
<td>
<span id="aliasesmark"></span>
<div class="declaration">
<code class="language-swift">Distance = IntMax</code>
</div>
</td>
</tr>


<tr>
<th>Import</th>
<td><code class="language-swift">import Swift</code></td>
</tr>

</table>


<h3>Initializers</h3>
<div class="declaration" id="init_">
<a class="toggle-link" data-toggle="collapse" href="#comment-init_">init(<wbr>_:)</a><div class="comment collapse" id="comment-init_"><div class="p">
    <p>Wrap and forward operations to <code>base</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init&lt;BaseIndex : BidirectionalIndex&gt;(_ base: BaseIndex)</code>

    </div></div>
</div>





<h3>Instance Methods</h3>
<div class="declaration inherited" id="func-advanced-by_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-advanced-by_">func advanced(<wbr>by:)</a>
        
<div class="comment collapse" id="comment-func-advanced-by_"><div class="p">
    <p>Returns the result of advancing <code>self</code> by <code>n</code> positions.</p>

<p><strong>Returns:</strong>
  - If <code>n &gt; 0</code>, the result of applying <code>successor</code> to <code>self</code> <code>n</code> times.
  - If <code>n &lt; 0</code>, the result of applying <code>predecessor</code> to <code>self</code> <code>-n</code> times.
  - Otherwise, <code>self</code>.</p>

<p><strong>Precondition:</strong> <code>n &gt;= 0</code> if only conforming to <code>ForwardIndex</code>
<strong>Complexity:</strong>
  - O(1) if conforming to <code>RandomAccessIndex</code>
  - O(<code>abs(n)</code>) otherwise</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func advanced(by n: AnyBidirectionalIndex.Distance) -&gt; AnyBidirectionalIndex</code>
    
        <h4>Declared In</h4>
        <a href="../../protocol/BidirectionalIndex/"><code>BidirectionalIndex</code></a>,    <a href="../../protocol/ForwardIndex/"><code>ForwardIndex</code></a>    
</div></div>
</div>
<div class="declaration inherited" id="func-advanced-by_limit_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-advanced-by_limit_">func advanced(<wbr>by:<wbr>limit:)</a>
        
<div class="comment collapse" id="comment-func-advanced-by_limit_"><div class="p">
    <p>Returns the result of advancing <code>self</code> by <code>n</code> positions, or until it
equals <code>limit</code>.</p>

<p><strong>Returns:</strong>
  - If <code>n &gt; 0</code>, the result of applying <code>successor</code> to <code>self</code> <code>n</code> times
    but not past <code>limit</code>.
  - If <code>n &lt; 0</code>, the result of applying <code>predecessor</code> to <code>self</code> <code>-n</code> times
    but not past <code>limit</code>.
  - Otherwise, <code>self</code>.</p>

<p><strong>Precondition:</strong> <code>n &gt;= 0</code> if only conforming to <code>ForwardIndex</code>.</p>

<p><strong>Complexity:</strong>
  - O(1) if conforming to <code>RandomAccessIndex</code>
  - O(<code>abs(n)</code>) otherwise</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func advanced(by n: AnyBidirectionalIndex.Distance, limit: AnyBidirectionalIndex) -&gt; AnyBidirectionalIndex</code>
    
        <h4>Declared In</h4>
        <a href="../../protocol/BidirectionalIndex/"><code>BidirectionalIndex</code></a>,    <a href="../../protocol/ForwardIndex/"><code>ForwardIndex</code></a>    
</div></div>
</div>
<div class="declaration inherited" id="func-distance-to_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-distance-to_">func distance(<wbr>to:)</a>
        
<div class="comment collapse" id="comment-func-distance-to_"><div class="p">
    <p>Measure the distance between <code>self</code> and <code>end</code>.</p>

<p><strong>Precondition:</strong>
  - <code>start</code> and <code>end</code> are part of the same sequence when conforming to
    <code>RandomAccessIndex</code>.
  - <code>end</code> is reachable from <code>self</code> by incrementation otherwise.</p>

<p><strong>Complexity:</strong>
  - O(1) if conforming to <code>RandomAccessIndex</code>
  - O(<code>n</code>) otherwise, where <code>n</code> is the function&#39;s result.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func distance(to end: AnyBidirectionalIndex) -&gt; AnyBidirectionalIndex.Distance</code>
    
        <h4>Declared In</h4>
        <a href="../../protocol/ForwardIndex/"><code>ForwardIndex</code></a>    
</div></div>
</div>
<div class="declaration" id="func-predecessor">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-predecessor">func predecessor()</a>
        
<div class="comment collapse" id="comment-func-predecessor"><div class="p">
    <p>Returns the previous consecutive value in a discrete sequence of
<code>AnyBidirectionalIndex</code> values.</p>

<p><strong>Precondition:</strong> <code>self</code> has a well-defined predecessor.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func predecessor() -&gt; AnyBidirectionalIndex</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-successor">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-successor">func successor()</a>
        
<div class="comment collapse" id="comment-func-successor"><div class="p">
    <p>Returns the next consecutive value in a discrete sequence of
<code>AnyBidirectionalIndex</code> values.</p>

<p><strong>Precondition:</strong> <code>self</code> has a well-defined successor.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func successor() -&gt; AnyBidirectionalIndex</code>
    
    
</div></div>
</div>


